/**
 * 
 */
package cn.getword.cnblogs.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Objects;

/**
 * <p>Title: NumUtils</p>
 * <p>Description: </p>
 * <p>Company: 江苏金恒信息科技有限公司</p>
 * @author Bruce Xu
 * @date 2014-2-26
 */
public class NumUtils {
	public static final BigDecimal ZERO = new BigDecimal("0");

	public static final BigDecimal ONE = new BigDecimal("1");

	// 数量小数位数
	public static final int QTYSCALE = 4;

	// 金额小数位数
	public static final int AMTSCALE = 2;

	// 报表数量小数位数
	public static final int RTP_QTY_SCALE = 4;

	// 报表金额小数位数
	public static final int RPT_AMT_SCALE = 2;

	// 单价小数位数
	public static final int PRICE_SCALE = 4;

	// 报表单价小数位数
	public static final int RPT_PRICE_SCALE = 4;

	private static final String UNIT = "万仟佰拾亿仟佰拾万仟佰拾元角分";

	private static final String DIGIT = "零壹贰叁肆伍陆柒捌玖";

	private static final double MAX_VALUE = 9999999999999.99D;

	/**
	 * @author sunanjun(012169) 2013-06-28 sunanjun@njsteel.com.cn
	 * @function 两个数相除
	 * @param amt1
	 * @param amt2
	 * @param scale
	 * @return
	 */
	public static BigDecimal divide(BigDecimal amt1, BigDecimal amt2, int scale) {
		if (amt1.compareTo(ZERO) == 0 || amt2.compareTo(ZERO) == 0) {
			return ZERO;
		}
		return amt1.divide(amt2, scale, BigDecimal.ROUND_HALF_UP);
	}

	/**
	 * @author sunanjun(012169) 2013-06-28 sunanjun@njsteel.com.cn
	 * @function 两个数相乘
	 * @param amt1
	 * @param amt2
	 * @param scale
	 * @return
	 */
	public static BigDecimal multiply(BigDecimal amt1, BigDecimal amt2, int scale) {
		return amt1.multiply(amt2).setScale(scale, BigDecimal.ROUND_HALF_UP);
	}

	/**
	 * 把字符型数字转换为整数。
	 * 
	 * @param input
	 * @return
	 * @author sunanjun(012169) 2013-06-28 sunanjun@njsteel.com.cn
	 */
	public static int objectConvertToInt(Object input) {
		int rtn = 0;
		try {
			rtn = Integer.parseInt(String.valueOf(input));
		} catch (NumberFormatException nfe) {
			rtn = 0;
		}
		return rtn;
	}

	/**
	 * 
	 *<p>Title: formatCurentcy</p>
	 *<p>Description: 根据指定规则格式化 数据</p>
	 *author：Bruce Xu
	 *@date: 2014-2-26
	 * @param num 需格式化的数据
	 * @param pattern 规则
	 * @return
	 */
	public static String formatCurentcy(String num, String pattern) {
		DecimalFormat df = new DecimalFormat(pattern);
		String result = df.format(Double.parseDouble(num));
		return result;
	}

	/**
	 * <p>Title: objToDecimal</p>
	 * <p>Description:对象转BigDecimal 多用于从map取值 </p>
	 * @author：Bruce Xu
	 * @date: 2014-3-31
	 * @param obj
	 * @return
	 */
	public static BigDecimal objToDecimal(Object obj) {
		try {
			if (Objects.equals(obj, null))
				return new BigDecimal(0);
			else
				return (BigDecimal) obj;
		} catch (Exception ex) {
			return BigDecimal.ZERO;
		}
	}

	/**
	 * 金额数字转大写中文数字
	 * @author WangWeiWei 020463
	 * @date 2019年3月25日 上午10:17:04
	 * @param v
	 * @return
	 */
	public static String numToUpperCN(double v) {
		if (v < 0 || v > MAX_VALUE) {
			return "参数非法!";
		}
		long l = Math.round(v * 100);
		if (l == 0) {
			return "零元整";
		}
		String strValue = l + "";
		int i = 0;
		int j = UNIT.length() - strValue.length();
		String rs = "";
		boolean isZero = false;
		for (; i < strValue.length(); i++, j++) {
			char ch = strValue.charAt(i);
			if (ch == '0') {
				isZero = true;
				if (UNIT.charAt(j) == '亿' || UNIT.charAt(j) == '万' || UNIT.charAt(j) == '元') {
					rs = rs + UNIT.charAt(j);
					isZero = false;
				}
			} else {
				if (isZero) {
					rs = rs + "零";
					isZero = false;
				}
				rs = rs + DIGIT.charAt(ch - '0') + UNIT.charAt(j);
			}
		}
		if (!rs.endsWith("分")) {
			rs = rs + "整";
		}
		rs = rs.replaceAll("亿万", "亿");
		return rs;
	}
	
	/**
	 * 数字转换成中文汉字
	 * 
	 * @param value
	 *            转换的数字
	 * @return 返回数字转后的汉字字符串
	 */
	public static String translate(int value) {
		String[] UNITS = { "", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", };
		String[] NUMS = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九", };
		// 转译结果
		String result = "";
		for (int i = String.valueOf(value).length() - 1; i >= 0; i--) {
			int r = (int) (value / Math.pow(10, i));
			result += NUMS[r % 10] + UNITS[i];
		}
		result = result.replaceAll("零[十, 百, 千]", "零");
		result = result.replaceAll("零+", "零");
		result = result.replaceAll("零([万, 亿])", "$1");
		result = result.replaceAll("亿万", "亿"); // 亿万位拼接时发生的特殊情况
		if (result.startsWith("一十"))
			result = result.substring(1);
		if (result.endsWith("零"))
			result = result.substring(0, result.length() - 1);
		return result;
	}
}
